/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pl.abstractvoid.gui;

import java.awt.Dimension;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.Window;
import java.io.IOException;
import java.io.InputStream;
import java.util.Scanner;
import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.border.Border;

/**
 * Collection of static methods that are often called by the components
 * @author Ivo Zieliński
 */
public class UtilsGUI {

    public static ImageIcon createImageIcon(String path) throws IOException {
        return new ImageIcon(createImage(path));
    }

    /**
     * Lodas image from Resources directory in the JAR
     * @param path
     * @return
     * @throws IOException 
     */
    public static Image createImage(String path) throws IOException {
        path = "/Images" + path;
        InputStream stream = MainFrame.class.getResourceAsStream(path);
        if (stream == null) {
            throw new IOException("Resource \"" + path + "\" not found.");
        }
        Image image = ImageIO.read(stream);
        return image;
    }

    /**
     * Creates a toolbar button for the MainFrame class
     * @param path
     * @param toolTip
     * @param format
     * @return
     * @throws IOException 
     */
    public static JButton createButton(String path, String toolTip, String format) 
            throws IOException {
        Border emptyBorder = BorderFactory.createEmptyBorder();
        JButton button = new JButton();
        button.setIcon(UtilsGUI.createImageIcon(path + "." + format));
        button.setBorder(emptyBorder);
        button.setOpaque(false);
        button.setContentAreaFilled(false);
        button.setRolloverEnabled(true);
        button.setRolloverIcon(UtilsGUI.createImageIcon(path + "Rollover." + format));
        button.setPressedIcon(UtilsGUI.createImageIcon(path + "Pressed." + format));
        button.setToolTipText(toolTip);
        return button;
    }

    /**
     * Places given Window in the center of the screen.
     *
     * @param window Window for centering.
     */
    public static void centerWindow(Window window) {
        Dimension dimensions = Toolkit.getDefaultToolkit().getScreenSize();
        int x = (dimensions.width - window.getWidth()) / 2;
        int y = (dimensions.height - window.getHeight()) / 2;
        if (x < 0) {
            x = 0;
        }
        if (y < 0) {
            y = 0;
        }
        window.setLocation(x, y);
    }

    /**
     * Read string from file.
     * @param path
     * @return
     * @throws IOException 
     */
    public static String readFile(String path)
            throws IOException {
        StringBuilder text = new StringBuilder();
        InputStream is = MainFrame.class.getResourceAsStream(path);
        if (is == null) {
            throw new IOException("Resource \"" + path + "\" not found.");
        }
        Scanner sc = new Scanner(is, "UTF-8");
        while (sc.hasNextLine()) {
            text.append(sc.nextLine());
        }
        return text.toString();
    }
}
